/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl;

import bl.AttendanceBO;
import common.CurrencyRenderer;
import entities.Attendance;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.TableRowSorter;
import model.AttendanceTableModel;

/**
 *
 * @author Sony
 */
public class AttendancePanel extends javax.swing.JPanel {

    /**
     * Creates new form AttendancePanel
     */
    AttendanceBO attBO = new AttendanceBO();
    AttendanceTableModel attModel;

    public AttendancePanel() {
        initComponents();


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jspAttendance = new javax.swing.JScrollPane();
        tblAttendance = new javax.swing.JTable();
        lblViewBy = new javax.swing.JLabel();
        cbxViewBy = new javax.swing.JComboBox();
        txtView = new javax.swing.JTextField();
        btnView = new javax.swing.JButton();
        lblShiftID = new javax.swing.JLabel();
        txtShiftID = new javax.swing.JTextField();
        lblDay = new javax.swing.JLabel();
        lblLaborID = new javax.swing.JLabel();
        txtLaborID = new javax.swing.JTextField();
        lblExtraShift = new javax.swing.JLabel();
        txtExtraShift = new javax.swing.JTextField();
        lblDailyAttendance = new javax.swing.JLabel();
        ckxDailyAttendance = new javax.swing.JCheckBox();
        btnEdit = new javax.swing.JButton();
        jdcDay = new com.toedter.calendar.JDateChooser();

        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "INFORMATION ATTENDANCE LABOR", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 0, 12))); // NOI18N

        tblAttendance.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "Title 6", "Title 7"
            }
        ));
        tblAttendance.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblAttendanceMouseClicked(evt);
            }
        });
        jspAttendance.setViewportView(tblAttendance);

        lblViewBy.setText("View by:");

        cbxViewBy.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All", "Labor", "Current Day" }));
        cbxViewBy.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbxViewByItemStateChanged(evt);
            }
        });

        txtView.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnView.setText("View");
        btnView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewActionPerformed(evt);
            }
        });

        lblShiftID.setText("Shift ID");

        txtShiftID.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblDay.setText("Day:");

        lblLaborID.setText("Labor ID");

        txtLaborID.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblExtraShift.setText("Extra Shift:");

        txtExtraShift.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblDailyAttendance.setText("Daily Attendance:");

        ckxDailyAttendance.setText("     ");
        ckxDailyAttendance.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        jdcDay.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jspAttendance)
            .addGroup(layout.createSequentialGroup()
                .addComponent(lblViewBy)
                .addGap(18, 18, 18)
                .addComponent(cbxViewBy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtView, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnView)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(lblLaborID)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtLaborID, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblDay)
                .addGap(19, 19, 19)
                .addComponent(jdcDay, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblDailyAttendance)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ckxDailyAttendance)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblShiftID)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtShiftID, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lblExtraShift)
                .addGap(18, 18, 18)
                .addComponent(txtExtraShift, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 142, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblViewBy)
                    .addComponent(cbxViewBy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtView, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnView))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jspAttendance, javax.swing.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblLaborID)
                        .addComponent(txtLaborID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblDay)
                        .addComponent(lblDailyAttendance)
                        .addComponent(ckxDailyAttendance)
                        .addComponent(lblShiftID)
                        .addComponent(txtShiftID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblExtraShift)
                        .addComponent(txtExtraShift, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnEdit))
                    .addComponent(jdcDay, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tblAttendanceMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblAttendanceMouseClicked
        // TODO add your handling code here:
        int i = tblAttendance.getSelectedRow();
        txtLaborID.setText((String) (tblAttendance.getValueAt(i, 0)));
        jdcDay.setDate((Date) (tblAttendance.getValueAt(i, 1)));
        ckxDailyAttendance.setSelected((boolean) tblAttendance.getValueAt(i, 2));
        txtShiftID.setText((String) tblAttendance.getValueAt(i, 3));
        txtExtraShift.setText((tblAttendance.getValueAt(i, 4)).toString());
    }//GEN-LAST:event_tblAttendanceMouseClicked

    private void btnViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewActionPerformed
        // TODO add your handling code here:
        int i = cbxViewBy.getSelectedIndex();
        switch (i) {
            case 0:
                attModel = new AttendanceTableModel(attBO.showAllAttendance());
                tblAttendance.setDefaultRenderer(Float.class, new CurrencyRenderer());
                tblAttendance.setRowSorter(new TableRowSorter<javax.swing.table.TableModel>(attModel));
                tblAttendance.setModel(attModel);
                break;
            case 1:
                String ID = txtView.getText();
                attModel = new AttendanceTableModel(attBO.showAttendanceByLabor(ID));
                tblAttendance.setDefaultRenderer(Float.class, new CurrencyRenderer());
                tblAttendance.setRowSorter(new TableRowSorter<javax.swing.table.TableModel>(attModel));
                tblAttendance.setModel(attModel);
                break;
            case 2:
                attModel = new AttendanceTableModel(attBO.showAttendanceCurrentDay());
                tblAttendance.setDefaultRenderer(Float.class, new CurrencyRenderer());
                tblAttendance.setRowSorter(new TableRowSorter<javax.swing.table.TableModel>(attModel));
                tblAttendance.setModel(attModel);
                break;
            default:
                JOptionPane.showMessageDialog(this, "Please Choose One Option!");
        }
    }//GEN-LAST:event_btnViewActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        Date getDay = jdcDay.getDate();
        Attendance attendance = new Attendance();

        attendance.setLaborID(txtLaborID.getText());
        attendance.setDay(getDay);
        attendance.setDailyAttendance(ckxDailyAttendance.isSelected());
        attendance.setShiftID(txtShiftID.getText());
        attendance.setExtraShift(Integer.valueOf(txtExtraShift.getText()));
        attendance.setSalaryDaily(attBO.calculateSalaryDaily(Integer.valueOf(txtExtraShift.getText()), attBO.getSalaryPerHourByID(txtLaborID.getText()), 8));

        if (attBO.editAttendance(attendance) == true) {
            attModel = new AttendanceTableModel(attBO.showAttendanceCurrentDay());
            tblAttendance.setDefaultRenderer(Float.class, new CurrencyRenderer());
            tblAttendance.setRowSorter(new TableRowSorter<javax.swing.table.TableModel>(attModel));
            tblAttendance.setModel(attModel);
            JOptionPane.showMessageDialog(this, "Success.");
        } else {
            JOptionPane.showMessageDialog(this, "Failed.");
        }
    }//GEN-LAST:event_btnEditActionPerformed

    private void cbxViewByItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbxViewByItemStateChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_cbxViewByItemStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnView;
    private javax.swing.JComboBox cbxViewBy;
    private javax.swing.JCheckBox ckxDailyAttendance;
    private com.toedter.calendar.JDateChooser jdcDay;
    private javax.swing.JScrollPane jspAttendance;
    private javax.swing.JLabel lblDailyAttendance;
    private javax.swing.JLabel lblDay;
    private javax.swing.JLabel lblExtraShift;
    private javax.swing.JLabel lblLaborID;
    private javax.swing.JLabel lblShiftID;
    private javax.swing.JLabel lblViewBy;
    private javax.swing.JTable tblAttendance;
    private javax.swing.JTextField txtExtraShift;
    private javax.swing.JTextField txtLaborID;
    private javax.swing.JTextField txtShiftID;
    private javax.swing.JTextField txtView;
    // End of variables declaration//GEN-END:variables
}
